package edu.bu.cs673.superkids123.children.view.lessons;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.controller.lessons.CountingLessonController;
import edu.bu.cs673.superkids123.common.model.Child;

/**
 * Class CountingLessonView
 * 
 * Supports the view part of the MVC pattern for a simple counting lesson for a
 * young child. Audio and visual cues provide an enhanced learning experience.
 * 
 * @author David Bardwell
 */
public class CountingLessonView extends AbstractNavigationView implements
		ActionListener {

	// Display groups of three numbers at a time like 1, 2, 3 or 4, 5, 6
	public static final int GROUP_OF_NUMBERS_TO_SHOW_AT_ONCE = 3;
	private static final float AVATAR_SCALE = 0.60f;
	private boolean readyToPaint = false;
	private boolean audioActive = false;
	private int currentAudioNumber = 1;
	private Child child;

	private JPanel countingPanel;
	private JPanel numberPanel;
	private JButton[] countingButtons = new JButton[GROUP_OF_NUMBERS_TO_SHOW_AT_ONCE];
	private JButton backButton;
	private JButton playAllButton;
	private JButton playButton;
	private JButton forwardButton;
	private JButton stopButton;
	private JPanel graphicsPanel;
	private NumberGraphics numberGraphics;
	private boolean isBusy = false;

	public CountingLessonView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	protected JPanel layout() {
		countingPanel = new JPanel() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			
			public void paint(Graphics g) {
				super.paint(g);
				if (readyToPaint) {
					int startingNumber = CountingLessonView.this.getMainFrame()
							.getController(CountingLessonController.class)
							.getCurrentStartingNumber();

					int positionsX[] = new int[3];
					positionsX[0] = countingButtons[0].getLocation().x + 20;
					positionsX[1] = countingButtons[1].getLocation().x + 20;
					positionsX[2] = countingButtons[2].getLocation().x + 20;

					int positionsY[] = new int[3];
					positionsY[0] = graphicsPanel.getLocation().y;
					positionsY[1] = positionsY[0] + 40;
					positionsY[2] = positionsY[1] + 40;
					numberGraphics = new NumberGraphics(positionsX, positionsY);
					numberGraphics.paintNumberGraphics(g, startingNumber,
							audioActive, currentAudioNumber);
				}
			}
		};

		this.setParentsButtonVisible(false);
		this.setAvatarVisible(true);
		
		
		countingPanel.setLayout(new GridBagLayout());
		child = (Child) authController.getCurrentUser();

		// Start Lesson
		getMainFrame().getController(CountingLessonController.class).lessonStart();

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 1;
		c.weighty = 0;
		c.insets = new Insets(5, 5, 5, 15);
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.NORTHWEST;

		JLabel repeatLabel = new JLabel("Repeat after me...");
		repeatLabel.setFont(new Font("Ariel", Font.PLAIN, 16));
		countingPanel.add(repeatLabel, c);
		c.gridy++;

		c.fill = GridBagConstraints.HORIZONTAL;

		// show the number images as buttons in a child numberPanel using
		// FlowLayout
		numberPanel = new JPanel();
		numberPanel.setLayout(new FlowLayout());

		initializeCountingButtons();
		showCountingButtons();
		countingPanel.add(numberPanel, c);

		c.gridy++;
		graphicsPanel = new JPanel();
		c.insets = new Insets(5, 5, 5, 5);
		countingPanel.add(graphicsPanel, c);

		JPanel buttonActionPanel = new JPanel();
		buttonActionPanel.setLayout(new FlowLayout());

		backButton = new JButton("<< Back");
		playAllButton = new JButton("Play All");
		playButton = new JButton("Play Screen");
		forwardButton = new JButton("Forward >>");

		buttonActionPanel.add(backButton);
		buttonActionPanel.add(playAllButton);
		buttonActionPanel.add(playButton);
		buttonActionPanel.add(forwardButton);

		backButton.addActionListener(this);
		playAllButton.addActionListener(this);
		playButton.addActionListener(this);
		forwardButton.addActionListener(this);

		// push the panel contents to the top of the panel
		c.gridy++;
		c.weighty = 1;
		countingPanel.add(new JPanel(), c);

		c.gridy++;
		c.insets = new Insets(5, 5, 15, 15);
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.SOUTH;
		c.weighty = 0;
		countingPanel.add(buttonActionPanel, c);

		// add a stop button
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.NONE;
		countingPanel.add(stopButton, c);

		readyToPaint = true;
		return countingPanel;
	}

	// Get the number buttons ready with their current images
	public void initializeCountingButtons() {
		readyToPaint = false;
		int j = 0;
		int currentStartingNumber = getMainFrame().getController(
				CountingLessonController.class).getCurrentStartingNumber();

		for (int i = currentStartingNumber; i < currentStartingNumber
				+ GROUP_OF_NUMBERS_TO_SHOW_AT_ONCE; i++) {
			countingButtons[j] = new JButton();
			ImageIcon numberImageIcon = getMainFrame().getController(
					CountingLessonController.class).getImageIcon(i);
			int width = numberImageIcon.getIconWidth();
			int height = numberImageIcon.getIconHeight();
			countingButtons[j].setBounds(new Rectangle(width, height));
			countingButtons[j].setIcon(numberImageIcon);
			countingButtons[j].addActionListener(this);
			j++;
		}
	}

	public void showCountingButtons() {
		numberPanel.removeAll();
		for (int i = 0; i < GROUP_OF_NUMBERS_TO_SHOW_AT_ONCE; i++) {
			numberPanel.add(countingButtons[i]);
		}
		readyToPaint = true;
		countingPanel.revalidate();
		countingPanel.repaint();
	}

	public void setAudioActive(boolean active) {
		audioActive = active;
	}

	public void setCurrentAudioNumber(int audioNumber) {
		currentAudioNumber = audioNumber;
	}

	public void repaintCountingPanel() {
		readyToPaint = true;
		countingPanel.revalidate();
		countingPanel.repaint();
	}

	public void setBusy(boolean busyState) {
		isBusy = busyState;
	}

	/**
	 * actionPerformed Delegate the responsibility of the action to the
	 * controller
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {

			if (e.getSource() == stopButton) {
				getMainFrame().getController(CountingLessonController.class)
						.setStopPlay(true);
			}

			// If the user is currently playing, then return
			if (isBusy) {
				return;
			}

			// play audio if child selects a number
			if (e.getSource() == countingButtons[0]) {
				getMainFrame().getController(CountingLessonController.class)
						.numberButtonPressed(0, this);
			} else if (e.getSource() == countingButtons[1]) {
				getMainFrame().getController(CountingLessonController.class)
						.numberButtonPressed(1, this);
			} else if (e.getSource() == countingButtons[2]) {
				getMainFrame().getController(CountingLessonController.class)
						.numberButtonPressed(2, this);
			}

			// manage control buttons
			else if (e.getSource() == backButton) {
				getMainFrame().getController(CountingLessonController.class)
						.backButtonPressed(this);
			} else if (e.getSource() == playAllButton) {
				getMainFrame().getController(CountingLessonController.class)
						.playAllButtonPressed(this);
			} else if (e.getSource() == playButton) {
				getMainFrame().getController(CountingLessonController.class)
						.playButtonPressed(this);
			} else if (e.getSource() == forwardButton) {
				getMainFrame().getController(CountingLessonController.class)
						.forwardButtonPressed(this);
			}
		}
	}

	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		return "Lets Count";
	}

	@Override
	public String getBorderTitle() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public boolean cleanupView() {
		
		getMainFrame().getController(CountingLessonController.class)
				.lessonEnd(false);

		return true;
	}
}